//注册页面


function reg(){
   let tel=doca('.input-id')
   let pwd=doca('.input-psd')
   let em=doca('.eml')
   let span=doca('#telReg')
   let span1=doca('#telReg1')

//    正则判断
let telReg = /^1[3-9]\d{9}$/
// 邮箱正则
let telRegEm= /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
tel.onfocus = ()=>{
    span.innerHTML = '请输入手机号'
    span.style.fontSize='10px';
}
tel.onblur = ()=>{
    if(telReg.test(tel.value)){
        span.innerHTML = '√'
        span.style.color='green'
        
    }else{
        span.innerHTML = '手机号格式不正确'
        span.style.fontSize='10px';
    }
}
    
em.onfocus = ()=>{
    span1.style.color=''
    span1.innerHTML = '请输入您的邮箱'
    span1.style.fontSize='10px';
}
em.onblur = ()=>{
    if(telRegEm.test(em.value)){
        span1.innerHTML = '√'
        span1.style.color='green'
        
    }else{
        span1.style.color=''
        span1.innerHTML = '邮箱格式不正确'
        span1.style.fontSize='10px';
        span1.style.color='red'
    }
}


  doca('.input-sub').onclick=function(){
    async function getGood(){
        tel=doca('.input-id').value
        pwd=doca('.input-psd').value
        em=doca('.eml').value
        let data= await axios.get({url:'http://localhost:53000/course'})
        // data = JSON.parse(data)
     
        
        let flag = false;
        data.forEach(item => {
            // console.log(item.name);
            if(item.name == tel) flag = true;        
        });
        
        if(flag){
        //   存在的话
        if(!tel.trim()||!pwd.trim()||!em.trim()) return;
        alert('用户名已存在')
       
        }else{
        //   不存在的话
        if(!tel.trim()||!pwd.trim()||!em.trim()) return;
        axios.post({url:'http://localhost:53000/course',data:{
                        "name":tel,
                        "password":pwd,
                        "mailbox":em
        }})
        alert('注册成功')
        location.href='./login2.html'
        }
    
   
    }
      getGood()
   }
  

}
reg();



// 获取节点方法
function doca (ele){
    return document.querySelector(ele)
}
function docall (ele){
    return document.querySelectorAll(ele)
}